package com.wa.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.List;

public class CustomSheetWriteHandler extends AbstractCellWriteHandler {

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        Sheet sheet = writeSheetHolder.getSheet();
        // 设置单元格下拉框
        CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 100, 0, 0);
        DataValidationHelper helper = sheet.getDataValidationHelper();
        // 数据可从数据库查询
        String[] array = {"测试1", "测试2", "测试3", "测试4"};
        DataValidationConstraint constraint = helper.createExplicitListConstraint(array);
        DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
        sheet.addValidationData(dataValidation);
    }
}
